package com.nidu.demo.post.executor.command;

import com.alibaba.cola.dto.SingleResponse;
import com.nidu.demo.company.ability.CompanyDomainService;
import com.nidu.demo.post.ability.PositionDomainService;
import com.nidu.demo.post.convertor.PositionConvertor;
import com.nidu.demo.post.dto.PositionAddCmd;
import com.nidu.demo.post.dto.PositionCO;
import com.nidu.demo.post.gateway.PositionGateway;
import com.nidu.demo.post.model.Position;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 职位创建命令执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class PositionAddCmdExe {

    private final PositionConvertor convertor;

    private final PositionGateway gateway;

    private final PositionDomainService domainService;

    private final CompanyDomainService companyDomainService;

    public SingleResponse<PositionCO> execute(PositionAddCmd cmd) {
        // 1. 转换为领域对象
        Position post = Position.create(cmd.getPostName(), cmd.getPostCode(), cmd.getCompanyId(), cmd.getRemark(), cmd.getSortOrder());

        // 2.检查公司是否存在
        companyDomainService.validateCompany(post.getCompanyId());
        // 3.检查对象是否已存在
        domainService.validateDuplicate(post.getPostCode(), post.getCompanyId(), post.getId());

        // 4. 调用网关保存
        Position save = gateway.create(post);

        // 5. 转换为DTO返回
        PositionCO PositionCO = convertor.toClientObject(save);

        return SingleResponse.of(PositionCO);
    }
} 